<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>xmake从入门到精通8：切换编译模式</title>
    <meta name="description" content="xmake是一个基于Lua的轻量级现代化c/c++的项目构建工具，主要特点是：语法简单易上手，提供更加可读的项目维护，实现跨平台行为一致的构建体验。本文我们会详细介绍下如何在项目构建过程中切换debug/release等常用构建模式，以及自定义其他编译模式。  项目源码  官方文档调试和发布模式通常，如果我们是通...">

    
    <meta name="keywords" content="xmake,lua,编译模式,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2019/12/05/quickstart-8-switch-build-mode/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>xmake从入门到精通8：切换编译模式</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2019-12-05
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#xmake" title="Category: xmake" rel="category">xmake</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a-->
        <a href="/cn/tag/#xmake" title="Tag: xmake" rel="tag">xmake</a>&nbsp;
    
        <!--a href="/tag/#lua" title="Tag: lua" rel="tag">lua</a-->
        <a href="/cn/tag/#lua" title="Tag: lua" rel="tag">lua</a>&nbsp;
    
        <!--a href="/tag/#%E7%BC%96%E8%AF%91%E6%A8%A1%E5%BC%8F" title="Tag: 编译模式" rel="tag">编译模式</a-->
        <a href="/cn/tag/#编译模式" title="Tag: 编译模式" rel="tag">编译模式</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>xmake是一个基于Lua的轻量级现代化c/c++的项目构建工具，主要特点是：语法简单易上手，提供更加可读的项目维护，实现跨平台行为一致的构建体验。</p>

<p>本文我们会详细介绍下如何在项目构建过程中切换debug/release等常用构建模式，以及自定义其他编译模式。</p>

<ul>
  <li><a href="https://github.com/xmake-io/xmake">项目源码</a></li>
  <li><a href="https://xmake.io/#/zh-cn/">官方文档</a></li>
</ul>

<h3 id="调试和发布模式">调试和发布模式</h3>

<p>通常，如果我们是通过<code class="highlighter-rouge">xmake create</code>命令创建的项目，会在xmake.lua里面自动添加一行编译规则的配置，如下：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_rules</span><span class="p">(</span><span class="s2">"mode.release"</span><span class="p">,</span> <span class="s2">"mode.debug"</span><span class="p">)</span>
<span class="n">target</span><span class="p">(</span><span class="s2">"hello"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
</code></pre>
</div>

<p>通过<code class="highlighter-rouge">add_rules</code>接口，我们默认添加了release和debug两个常用的内置规则，它们会在编译的时候附带上对应模式相关的一些编译flags，来开启优化用于发布或者调试编译。</p>

<p>如果仅仅执行了<code class="highlighter-rouge">xmake</code>命令，没有额外的配置，那么默认就会是release编译，等价于：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -m release
<span class="gp">$ </span>xmake
<span class="o">[</span>  0%]: ccache compiling.release src/main.cpp
<span class="o">[</span>100%]: linking.release <span class="nb">test
</span>build ok!
</code></pre>
</div>

<p>如果我们要切换到debug编译模式，只需要：</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -m debug
<span class="gp">$ </span>xmake
<span class="o">[</span>  0%]: ccache compiling.debug src/main.cpp
<span class="o">[</span>100%]: linking.debug <span class="nb">test
</span>build ok!
</code></pre>
</div>

<p>上面的<code class="highlighter-rouge">-m/--mode=</code>参数就是用来设置编译模式，会跟<code class="highlighter-rouge">mode.release</code>和<code class="highlighter-rouge">mode.debug</code>这两个规则做关联。</p>

<p>那么，他们是如何关联上的呢？我们可以先来看下这两个规则的内部实现：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">rule</span><span class="p">(</span><span class="s2">"mode.debug"</span><span class="p">)</span>
    <span class="n">after_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span> <span class="k">then</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">target</span><span class="p">:</span><span class="n">get</span><span class="p">(</span><span class="s2">"symbols"</span><span class="p">)</span> <span class="k">then</span>
                <span class="n">target</span><span class="p">:</span><span class="n">set</span><span class="p">(</span><span class="s2">"symbols"</span><span class="p">,</span> <span class="s2">"debug"</span><span class="p">)</span>
            <span class="k">end</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">target</span><span class="p">:</span><span class="n">get</span><span class="p">(</span><span class="s2">"optimize"</span><span class="p">)</span> <span class="k">then</span>
                <span class="n">target</span><span class="p">:</span><span class="n">set</span><span class="p">(</span><span class="s2">"optimize"</span><span class="p">,</span> <span class="s2">"none"</span><span class="p">)</span>
            <span class="k">end</span>
        <span class="k">end</span>
    <span class="k">end</span><span class="p">)</span>

<span class="n">rule</span><span class="p">(</span><span class="s2">"mode.release"</span><span class="p">)</span>
    <span class="n">after_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"release"</span><span class="p">)</span> <span class="k">then</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">target</span><span class="p">:</span><span class="n">get</span><span class="p">(</span><span class="s2">"symbols"</span><span class="p">)</span> <span class="ow">and</span> <span class="n">target</span><span class="p">:</span><span class="n">targetkind</span><span class="p">()</span> <span class="o">~=</span> <span class="s2">"shared"</span> <span class="k">then</span>
                <span class="n">target</span><span class="p">:</span><span class="n">set</span><span class="p">(</span><span class="s2">"symbols"</span><span class="p">,</span> <span class="s2">"hidden"</span><span class="p">)</span>
            <span class="k">end</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">target</span><span class="p">:</span><span class="n">get</span><span class="p">(</span><span class="s2">"optimize"</span><span class="p">)</span> <span class="k">then</span>
                <span class="k">if</span> <span class="n">is_plat</span><span class="p">(</span><span class="s2">"android"</span><span class="p">,</span> <span class="s2">"iphoneos"</span><span class="p">)</span> <span class="k">then</span>
                    <span class="n">target</span><span class="p">:</span><span class="n">set</span><span class="p">(</span><span class="s2">"optimize"</span><span class="p">,</span> <span class="s2">"smallest"</span><span class="p">)</span>
                <span class="k">else</span>
                    <span class="n">target</span><span class="p">:</span><span class="n">set</span><span class="p">(</span><span class="s2">"optimize"</span><span class="p">,</span> <span class="s2">"fastest"</span><span class="p">)</span>
                <span class="k">end</span>
            <span class="k">end</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">target</span><span class="p">:</span><span class="n">get</span><span class="p">(</span><span class="s2">"strip"</span><span class="p">)</span> <span class="k">then</span>
                <span class="n">target</span><span class="p">:</span><span class="n">set</span><span class="p">(</span><span class="s2">"strip"</span><span class="p">,</span> <span class="s2">"all"</span><span class="p">)</span>
            <span class="k">end</span>
        <span class="k">end</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

<p>可以看到，在target被加载阶段，xmake会去判断用户对<code class="highlighter-rouge">xmake f --mode=xxx</code>的参数配置，如果通过<code class="highlighter-rouge">is_mode()</code>接口获取到是debug模式，那么会禁用相关优化并且启用符号输出。
而如果是release模式，那么会开启编译优化并且strip掉所有调试符号。</p>

<h3 id="定制化的模式配置">定制化的模式配置</h3>

<p>当然，内置的这两规则默认设置的这些编译配置，只能满足大部分场景的常规需求，如果用户想要在不同的编译模式下定制化一些个人的编译配置，那么需要自己在xmake.lua做判断。</p>

<p>例如，我们想在release下也启用调试符号，那么只需要：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"release"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">set_symbols</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<p>或者额外增加一些编译flags：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"release"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">add_cflags</span><span class="p">(</span><span class="s2">"-fomit-frame-pointer"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<p>注：如果用户自己的配置和<code class="highlighter-rouge">mode.release</code>内置的配置冲突，会优先使用用户的设置。</p>

<p>当然，我们也可以完全不去通过<code class="highlighter-rouge">add_rules("mode.debug", "mode.release")</code>添加默认的配置规则，让用户完全自己控制模式配置：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="c1">-- 如果当前编译模式是debug</span>
<span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span> <span class="k">then</span>

    <span class="c1">-- 添加DEBUG编译宏</span>
    <span class="n">add_defines</span><span class="p">(</span><span class="s2">"DEBUG"</span><span class="p">)</span>

    <span class="c1">-- 启用调试符号</span>
    <span class="n">set_symbols</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span>

    <span class="c1">-- 禁用优化</span>
    <span class="n">set_optimize</span><span class="p">(</span><span class="s2">"none"</span><span class="p">)</span>
<span class="k">end</span>

<span class="c1">-- 如果是release或者profile模式</span>
<span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"release"</span><span class="p">,</span> <span class="s2">"profile"</span><span class="p">)</span> <span class="k">then</span>

    <span class="c1">-- 如果是release模式</span>
    <span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"release"</span><span class="p">)</span> <span class="k">then</span>

        <span class="c1">-- 隐藏符号</span>
        <span class="n">set_symbols</span><span class="p">(</span><span class="s2">"hidden"</span><span class="p">)</span>

        <span class="c1">-- strip所有符号</span>
        <span class="n">set_strip</span><span class="p">(</span><span class="s2">"all"</span><span class="p">)</span>

        <span class="c1">-- 忽略帧指针</span>
        <span class="n">add_cxflags</span><span class="p">(</span><span class="s2">"-fomit-frame-pointer"</span><span class="p">)</span>
        <span class="n">add_mxflags</span><span class="p">(</span><span class="s2">"-fomit-frame-pointer"</span><span class="p">)</span>

    <span class="c1">-- 如果是profile模式</span>
    <span class="k">else</span>
        <span class="c1">-- 启用调试符号</span>
        <span class="n">set_symbols</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span>
    <span class="k">end</span>

    <span class="c1">-- 添加扩展指令集</span>
    <span class="n">add_vectorexts</span><span class="p">(</span><span class="s2">"sse2"</span><span class="p">,</span> <span class="s2">"sse3"</span><span class="p">,</span> <span class="s2">"ssse3"</span><span class="p">,</span> <span class="s2">"mmx"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<h3 id="其他内置模式规则">其他内置模式规则</h3>

<p>通过上文的例子，我们看到除了debug/release模式，还加了个profile模式的配置判断，其实xmake也提供了对应的内置模式，还有哪些，我们具体来看下：</p>

<h4 id="modedebug">mode.debug</h4>

<p>为当前工程xmake.lua添加debug编译模式的配置规则，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_rules</span><span class="p">(</span><span class="s2">"mode.debug"</span><span class="p">)</span>
</code></pre>
</div>

<p>相当于：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">set_symbols</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span>
    <span class="n">set_optimize</span><span class="p">(</span><span class="s2">"none"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<p>我们可以通过：<code class="highlighter-rouge">xmake f -m debug</code>来切换到此编译模式。</p>

<h4 id="moderelease">mode.release</h4>

<p>为当前工程xmake.lua添加release编译模式的配置规则，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_rules</span><span class="p">(</span><span class="s2">"mode.release"</span><span class="p">)</span>
</code></pre>
</div>

<p>相当于：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"release"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">set_symbols</span><span class="p">(</span><span class="s2">"hidden"</span><span class="p">)</span>
    <span class="n">set_optimize</span><span class="p">(</span><span class="s2">"fastest"</span><span class="p">)</span>
    <span class="n">set_strip</span><span class="p">(</span><span class="s2">"all"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<p>我们可以通过：<code class="highlighter-rouge">xmake f -m release</code>来切换到此编译模式。</p>

<h4 id="modecheck">mode.check</h4>

<p>为当前工程xmake.lua添加check编译模式的配置规则，一般用于内存检测，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_rules</span><span class="p">(</span><span class="s2">"mode.check"</span><span class="p">)</span>
</code></pre>
</div>

<p>相当于：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"check"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">set_symbols</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span>
    <span class="n">set_optimize</span><span class="p">(</span><span class="s2">"none"</span><span class="p">)</span>
    <span class="n">add_cxflags</span><span class="p">(</span><span class="s2">"-fsanitize=address"</span><span class="p">,</span> <span class="s2">"-ftrapv"</span><span class="p">)</span>
    <span class="n">add_mxflags</span><span class="p">(</span><span class="s2">"-fsanitize=address"</span><span class="p">,</span> <span class="s2">"-ftrapv"</span><span class="p">)</span>
    <span class="n">add_ldflags</span><span class="p">(</span><span class="s2">"-fsanitize=address"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<p>我们可以通过：<code class="highlighter-rouge">xmake f -m check</code>来切换到此编译模式。</p>

<h4 id="modeprofile">mode.profile</h4>

<p>为当前工程xmake.lua添加profile编译模式的配置规则，一般用于性能分析，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_rules</span><span class="p">(</span><span class="s2">"mode.profile"</span><span class="p">)</span>
</code></pre>
</div>

<p>相当于：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"profile"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">set_symbols</span><span class="p">(</span><span class="s2">"debug"</span><span class="p">)</span>
    <span class="n">add_cxflags</span><span class="p">(</span><span class="s2">"-pg"</span><span class="p">)</span>
    <span class="n">add_ldflags</span><span class="p">(</span><span class="s2">"-pg"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<p>我们可以通过：<code class="highlighter-rouge">xmake f -m profile</code>来切换到此编译模式。</p>

<h4 id="modecoverage">mode.coverage</h4>

<p>为当前工程xmake.lua添加coverage编译模式的配置规则，一般用于覆盖分析，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">add_rules</span><span class="p">(</span><span class="s2">"mode.coverage"</span><span class="p">)</span>
</code></pre>
</div>

<p>相当于：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"coverage"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">add_cxflags</span><span class="p">(</span><span class="s2">"--coverage"</span><span class="p">)</span>
    <span class="n">add_mxflags</span><span class="p">(</span><span class="s2">"--coverage"</span><span class="p">)</span>
    <span class="n">add_ldflags</span><span class="p">(</span><span class="s2">"--coverage"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<p>我们可以通过：<code class="highlighter-rouge">xmake f -m coverage</code>来切换到此编译模式。</p>

<p>注：生成的gcno文件一般都是个obj所在目录对应的哦，因此需要从build目录下去找。</p>

<h3 id="扩展自己的编译模式">扩展自己的编译模式</h3>

<p>xmake的模式配置，并没有固定值，用户可以随意传入和配置，只要<code class="highlighter-rouge">xmake f -m/--mode=xxx</code>传入的模式值和xmake.lua里面的<code class="highlighter-rouge">is_mode("xxx")</code>能对应上就行。</p>

<p>比如，我们设置了一个自己独有的编译模式<code class="highlighter-rouge">my_mode</code>，可以直接在命令行配置切换；</p>

<div class="language-bash highlighter-rouge"><pre class="highlight"><code><span class="gp">$ </span>xmake f -m my_mode
<span class="gp">$ </span>xmake
<span class="o">[</span>  0%]: ccache compiling.my_mode src/main.cpp
<span class="o">[</span>100%]: linking.my_mode <span class="nb">test
</span>build ok!
</code></pre>
</div>

<p>然后xmake.lua里面对相应的值进行判断即可：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"my_mode"</span><span class="p">)</span> <span class="k">then</span>
    <span class="n">add_defines</span><span class="p">(</span><span class="s2">"ENABLE_MY_MODE"</span><span class="p">)</span>
<span class="k">end</span>
</code></pre>
</div>

<h3 id="使用模式变量">使用模式变量</h3>

<p>我们也可以直接在配置值中传递模式变量<code class="highlighter-rouge">$(mode)</code>，比如根据不同模式选择链接不同的库：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">add_links</span><span class="p">(</span><span class="s2">"xxx_$(mode)"</span><span class="p">)</span>
</code></pre>
</div>

<p>上面的配置，如果是调试模式编译就会选择链接：<code class="highlighter-rouge">libxxx_debug.a</code>库，而release下就会链接<code class="highlighter-rouge">libxxx_release.a</code>，当然，我们也可以设置到库搜索路径中，根据目录来选择对应的库。</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">add_linkdirs</span><span class="p">(</span><span class="s2">"lib/$(mode)"</span><span class="p">)</span>
    <span class="n">add_links</span><span class="p">(</span><span class="s2">"xxx"</span><span class="p">)</span>
</code></pre>
</div>

<p>另外，我们可以通过<code class="highlighter-rouge">get_config("mode")</code>直接获取到传入的模式配置值，并且这几种获取方式，在自定义脚本也是同样有效的哦，例如：</p>

<div class="language-lua highlighter-rouge"><pre class="highlight"><code><span class="n">target</span><span class="p">(</span><span class="s2">"test"</span><span class="p">)</span>
    <span class="n">set_kind</span><span class="p">(</span><span class="s2">"binary"</span><span class="p">)</span>
    <span class="n">add_files</span><span class="p">(</span><span class="s2">"src/*.c"</span><span class="p">)</span>
    <span class="n">on_load</span><span class="p">(</span><span class="k">function</span> <span class="p">(</span><span class="n">target</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">is_mode</span><span class="p">(</span><span class="s2">"release"</span><span class="p">)</span> <span class="k">then</span>
            <span class="nb">print</span><span class="p">(</span><span class="n">get_config</span><span class="p">(</span><span class="s2">"mode"</span><span class="p">),</span> <span class="s2">"$(mode)"</span><span class="p">)</span>
        <span class="k">end</span>
    <span class="k">end</span><span class="p">)</span>
</code></pre>
</div>

        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        

        
        
            
        
            
            
            
                
                    
                        
                        <h2 id="similar_posts">相关文章</h2>
                        <ul>
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/17/xmake-update-v2.6.2/">xmake v2.6.2 发布，新增 Linux 内核驱动模块构建支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/12/03/xmake-update-v2.6.1/">xmake v2.6.1 发布，使用 Lua5.4 运行时，Rust 和 C++ 混合编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/30/xmake-update-v2.5.9/">xmake v2.5.9 发布，改进 C++20 模块，并支持 Nim, Keil MDK 和 Unity Build
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/10/08/xmake-update-v2.5.8/">xmake v2.5.8 发布，新增 Pascal/Swig 程序和 Lua53 运行时支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                        
                        <li class="relatedPost">
                            <a href="/cn/2021/08/29/xmake-update-v2.5.7/">xmake v2.5.7 发布，包依赖锁定和 Vala/Metal 语言编译支持
                            
                            </a>
                        </li>
                        
                        
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
        
            </ul>
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2019/11/30/quickstart-7-build-cuda-project/">xmake从入门到精通7：开发和构建Cuda程序</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2019/12/05/quickstart-9-cross-compile/">xmake从入门到精通9：交叉编译详解</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
